define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, T_f_password, $interval, GoDialog, GoPage, Http,Auth) {
		
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.updateVerifycode();
		}, false)
		
		$scope.input = {
			'mobile': "",
			'password': "",
			'password2': "",
			'cellphonevcode': "",
		}
		$scope.showGetRegcode = true;
		$scope.regCodeString = '获取验证码';

		//120s倒计时--张
		$scope.otherString ='s后重新获取'; 
		$scope.time = "";
		function showWaitingRegcode() {
			$scope.showGetRegcode = false;

			$scope.time = 120;
			var stop = $interval(function(){
				if($scope.time>0) {
					$scope.time = $scope.time-1;
				}else{
					$interval.cancel(stop);
					$scope.showGetRegcode = true;
					$scope.time = '';
					$scope.regCodeString ='请重新获取'; 
				}
			},1000);
		}
		
		//获取验证码（非手机）
		$scope.updateVerifycode = function() {
//			if (!$scope.input.mobile) {
//				GoDialog.toast("请输入手机号码！");
//				$scope.inSubmit = false;
//				return;
//			}
			var session = Auth.get_user_id();
			if(session == "")
				session = Auth.get_session();
			var args = {
//				"mobile": $scope.input.mobile,
				"ts": new Date().getTime(),//这个是必须的
				"method": 'member.verifyCodeMobile',
				"session": session
			};
			//提交数据
			var post_data = {
				sign: Http.sign(args),
				method: args.method,
				api_version: '1.0'
			};

			for(var k in args) {
				post_data[k] = args[k]
			}

			var getparam = '';
			for(var k in post_data) {
				if(!getparam) {
					getparam += '?';
				} else {
					getparam += '&';
				}
				getparam += k + '=' + encodeURIComponent(post_data[k]);
			}
			$scope.input.graphverifycode_url = HTTP_URL + getparam;//验证码(非手机)
		}
		
		//获取验证码（手机）
		function getCellphoneCode(mobile, verifycode) {
			if($scope.inSubmit) {
				return;
			}
			$scope.inSubmit = true;

			T_f_password.get_CellphoneCode({
				'mobile': mobile,
				'verifycode': verifycode,
				"type": 'findpass'
			}, function(res) {
				if(!res.success) {
					GoDialog.alert('设置密码失败', res.msg);
					return;
				}
				showWaitingRegcode();
			}, postError, function() {
				$scope.inSubmit = false;
			})
		}
		
		/**
		 * 获取验证码
		 */
		$scope.onGetRegcode = function() {
			var input = $scope.input;
			if(!input) return;

			input.mobile = (input.mobile || '').trim();
			if(!input.mobile) {
				GoDialog.alert("输入不正确", "请输入手机号码");
				$scope.inSubmit = false;
				return;
			}

			input.graphverifycode = (input.graphverifycode || '').trim();
			if(!input.graphverifycode) {
				GoDialog.alert("输入不正确", "请输入图形验证码");
				$scope.inSubmit = false;
				return;
			}
			getCellphoneCode(input.mobile, input.graphverifycode);//获取验证码（手机）
		}
		
		
		//请求错误
		function postError() {
			GoDialog.toast("System error, try again...");
		}

		function checkCellphoneCode(mobile, cellphonevcode, callback) {
			if ($scope.inSubmit) {
				return;
			}
			$scope.inSubmit = true;

			T_f_password.check_CellphoneCode({
				'mobile': mobile, 'cellphonevcode': cellphonevcode
			}, function(res) {
				if (!res.success) {
					$scope.inSubmit = false;
					GoDialog.toast(res.msg);
					return;
				}

				callback && callback();
			}, function() {
				$scope.inSubmit = false;
				postError();
			})
		}

		function recover(mobile, password) {
			T_f_password.recover({
				'mobile': mobile, 'password': password
			}, function(res) {
				if (!res.success) {
					$scope.inSubmit = false;
					GoDialog.toast(res.msg);
					return;
				}

				GoDialog.toast("修改成功");

				$scope.input.password = "";
				$scope.input.password2 = "";
				$scope.input.cellphonevcode = "";

				GoPage.go_back();
			}, postError, function() {
				$scope.inSubmit = false;
			})
		}

		$scope.onRecover = function() {
			var input = $scope.input;
			if (!input) return;

			if (!input.mobile) {
				GoDialog.toast("请输入手机号码");
				return;
			} else if (!input.cellphonevcode) {
				GoDialog.toast("请输入手机验证码");
				return;
			} else if (!input.password) {
				GoDialog.toast("请输入新密码");
				return;
			} else if (!input.password2) {
				GoDialog.toast("请输入确认密码");
				return;
			} else if (input.password != input.password2) {
				GoDialog.toast("请确认两次密码输入一致");
				return;
			}

			checkCellphoneCode(input.mobile, input.cellphonevcode, function() {
				recover(input.mobile, input.password);
			})
		}
		
		//密码是否可见
		$scope.inputType = "password";
		$scope.showPassword = function() {
			$scope.inputType = $scope.inputType == "password" ? "text" : "password";
		}
		//密码是否可见
		$scope.inputType2 = "password";
		$scope.showPassword2 = function() {
			$scope.inputType2 = $scope.inputType2 == "password" ? "text" : "password";
		}

	}

	controller.$inject = ['$scope', 'T_f_password','$interval', 'GoDialog', 'GoPage', 'Http','Auth'];
	
	return app.registerController("t_f_passwordctrl",controller);
});